﻿using System;
using System.ComponentModel;

using DevExpress.Xpo;
using DevExpress.Data.Filtering;

using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;


namespace ECRM.Module
{
    [Custom("Caption", "单据编号设置")]
    [NavigationItem("系统设置")]
    [DefaultProperty("SeedType")]
    public class SeedNum : XPBaseObject
    {
        protected SeedNum(Session session) : base(session) { }

        public SeedNum(Session session, string seedType)
            : base(session)
        {
            _seedType = seedType;
        }

        private string _seedType;
        [Custom("Caption", "单据类型")]
        [DevExpress.Xpo.Key(false)]
        [Custom("AllowEdit", "False")]
        public string SeedType
        {
            get
            {
                return _seedType;
            }
            set
            {
                SetPropertyValue("SeedType", ref _seedType, value);
            }
        }

        private string _seedCaption;
        [Custom("Caption", "单据描述")]
        public string SeedCaption
        {
            get
            {
                return _seedCaption;
            }
            set
            {
                SetPropertyValue("SeedCaption", ref _seedCaption, value);
            }
        }
        private string _Prefix;
        [Custom("Caption", "单据编号前缀")]
        public string Prefix
        {
            get
            {
                return _Prefix;
            }
            set
            {
                SetPropertyValue("Prefix", ref _Prefix, value);
            }
        }
        private int _Length = 4;
        [Custom("Caption", "流水号位数")]
        [Custom("AllowEdit", "False")]
        public int Length
        {
            get
            {
                return _Length;
            }
            set
            {
                SetPropertyValue("Length", ref _Length, value);
            }
        }

        private int _Seed = 0;
        [Custom("Caption", "种子")]
        [Custom("AllowEdit", "False")]
        public int Seed
        {
            get
            {
                return _Seed;
            }
            set
            {
                SetPropertyValue("Seed", ref _Seed, value);
            }
        }
        private DateTime _SeedDate = DateTime.Now;
        [Custom("AllowEdit", "False")]
        public DateTime SeedDate
        {
            get
            {
                return _SeedDate;
            }
            set
            {
                SetPropertyValue("SeedDate", ref _SeedDate, value);
            }
        }
        public static string GetSeedNum(Session session, string seedType)
        {
            SeedNum seednum = session.FindObject<SeedNum>(new BinaryOperator("SeedType", seedType));
            if (seednum == null)
            {
                seednum = new SeedNum(session);
                seednum.SeedType = seedType;
                seednum.Length = 12;
                seednum.Save();
                return String.Format("{0}{1}", seednum.SeedDate.ToString("yyyyMMdd"), seednum.Seed.ToString("0000"));
            }
            else
            {
                DateTime now = DateTime.Now;
                if (seednum.SeedDate.Date != now.Date)
                {
                    seednum.Seed = 1;
                }
                else
                {
                    seednum.Seed++;
                }
                seednum.SeedDate = now;

                return string.Format("{0}{1}{2}", seednum.Prefix, seednum.SeedDate.ToString("yyyyMMdd"), seednum.Seed.ToString("0000"));
            }
        }
    }

    [NavigationItem("系统设置")]
    [Custom("Caption", "币别设置")]
    [DefaultProperty("CurrencyName")]
    public class Currency : BaseObject
    {
        public Currency(Session session)
            : base(session)
        {

        }
        private string _currencyName;
        [Custom("Caption", "币别")]
        [RuleRequiredField]
        public string CurrencyName
        {
            get { return _currencyName; }
            set { SetPropertyValue<string>("CurrencyName", ref _currencyName, value); }
        }
        private string _description;
        [Custom("Caption", "描述")]
        public string Description
        {
            get { return _description; }
            set { SetPropertyValue<string>("Description", ref _description, value); }
        }
    }

    public class ConstString
    {
        public static readonly string EmptyOrderNO = "自动编号";
    }
}
